package com.ma4567.remotecontroller.SmallTools.Calendar;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.MysqlHelper.CalendarTodo;
import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.ffmpegManager.ffmpegController;
import com.ma4567.remotecontroller.util.CustomRespone;
import com.ma4567.remotecontroller.util.Matters;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.ArrayList;

class TodoResponse extends CustomRespone{
    public ArrayList<Matters> matters;

    public TodoResponse(int status, ArrayList<Matters> matters){
        super(status);
        this.matters = matters;
    }
}

@CrossOrigin
@RestController
public class GetTodoList {
    @RequestMapping(value = "/api/getTodo", method = RequestMethod.GET)
    public String GetTodoList(HttpServletRequest request,
                              @RequestParam("cookie") String Cookie,
                              @RequestParam("gmt") String GMT) throws SQLException {
        ffmpegController.StopStream();
        TableRequestLog.AddLog("Request todolist", Cookie, request.getRemoteAddr());
        if(!RemoteControllerApplication.userManager.JudgeCookie(Cookie)){//鉴cookie
            return JSONObject.toJSONString(new TodoResponse(1, null), SerializerFeature.PrettyFormat);
        }
        else{
            int year = Integer.parseInt(GMT.split(" ")[3]);
            int day = Integer.parseInt(GMT.split(" ")[2]);
            int month = 0;
            switch (GMT.split(" ")[1]) {
                case "Jan" -> month = 1;
                case "Feb" -> month = 2;
                case "Mar" -> month = 3;
                case "Apr" -> month = 4;
                case "May" -> month = 5;
                case "Jun", "June" -> month = 6;
                case "Jul" -> month = 7;
                case "Aug" -> month = 8;
                case "Sep" -> month = 9;
                case "Oct" -> month = 10;
                case "Nov" -> month = 11;
                case "Dec" -> month = 12;
            }
            ArrayList<Matters> r = CalendarTodo.getMattersByDate(RemoteControllerApplication.userManager.getUserByCookie(Cookie).Account, year, month, day);
            return JSONObject.toJSONString(new TodoResponse(0, r), SerializerFeature.PrettyFormat);
        }
    }
}
